﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using pay.Entity;
using System.Data.SqlClient;

namespace pay.DAL
{
    public class OrderDetailDAL
    {
        /// <summary>
        /// Web_OrderDetail对象的添加方法
        /// </summary>
        /// <param name="t">Web_OrderDetail实体对象</param>
        /// <returns>数据库影响行数</returns>
        public int Add(OrderDetail t)
        {
            try
            {
                Tools tool = new Tools();
                
                tool.AddSqlParameter("id", SqlDbType.Int, 4, t.Id);
                tool.AddSqlParameter("ordernumber", SqlDbType.VarChar, 30, t.Ordernumber);
                tool.AddSqlParameter("productid", SqlDbType.Int, 4, t.Productid);
                tool.AddSqlParameter("num", SqlDbType.Int, 4, t.Num);
                tool.AddSqlParameter("singleprice", SqlDbType.Money, 8, t.Singleprice);
                tool.AddSqlParameter("isonline", SqlDbType.TinyInt, 1, t.Isonline);
                tool.AddSqlParameter("nowprice", SqlDbType.Money, 8, t.Nowprice);
                tool.AddSqlParameter("issell", SqlDbType.VarChar, 2, t.Issell);
                return Emoney.DataAccess.SqlHelper.ExecuteNonQuery(DBConn.Con_Etone, CommandType.StoredProcedure, "Web_OrderDetail_Add", tool.GetSqlParameters());
            }
            catch (Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "Add", LogYuMin.LogStates.ERROR, ex);
                return 0;
            }
           
        }

        public List<OrderDetails> GetOrderDetailByID(OrderQuery query)
        {
            Tools tool = new Tools();
            List<OrderDetails> list = new List<OrderDetails>();
            tool.AddSqlParameter("ordernumber", SqlDbType.VarChar, 50, query.OrderNumber);
            SqlConnection con = new SqlConnection(DBConn.Con_emweb_new);
            con.Open();
            SqlDataReader reader = Emoney.DataAccess.SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "Web_OrderDetail_ByID", tool.GetSqlParameters());
            OrderDetails m = null;
            try
            {
                while (reader.Read())
                {
                    m = new OrderDetails();
                    m.Id = Tools.GetInt(reader["id"]);
                    m.Ordernumber = Tools.GetString(reader["ordernumber"]);
                    m.Productid = Tools.GetInt(reader["productid"]);
                    m.Num = Tools.GetInt(reader["num"]);
                    m.Singleprice = Tools.GetDouble(reader["singleprice"]);
                    m.Isonline = Tools.GetInt(reader["isonline"]);
                    m.Nowprice = Tools.GetDouble(reader["nowprice"]);
                    m.Issell = Tools.GetString(reader["issell"]);
                    m.Productname = Tools.GetString(reader["productname"]);
                    m.Allmoney = Tools.GetDouble(reader["allmoney"]);
                    list.Add(m);
                    
                }
                return list;
            }
            catch (Exception ex)
            {

                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "GetOrderByOrdernumber", LogYuMin.LogStates.ERROR, ex);
          
            }
            finally
            {
                try
                {
                    reader.Close();
                    con.Close();
                }
                catch
                {
                }
            }
            return null;
        }



        public string GetActiveUserPwd1(string mobile,int timetype)
        {
           string reValue = "";
           SqlConnection con = new SqlConnection("server=192.168.3.14;database=emoney_streak;uid=wyw;pwd=emoney.cn");
           con.Open();
           try
            {
                SqlCommand cmd = new SqlCommand("SP_Pro_WebActiveUserTimeRights", con); //10.252
                cmd.CommandType = CommandType.StoredProcedure;
                //添加存储过程输入输出参数类型及输入参数值
                cmd.Parameters.Add("@pMob", SqlDbType.VarChar, 20).Value = mobile;
                cmd.Parameters.Add("@pTimeType", SqlDbType.Int).Value = timetype;
                cmd.Parameters.Add("@opPwd", SqlDbType.VarChar, 8);
                cmd.Parameters.Add("@opMsg", SqlDbType.VarChar, 64);
                cmd.Parameters.Add("@opEndTimeNew", SqlDbType.DateTime);
                //cmd.Parameters["@opEndTimeNew"].DbType = DbType.String;
                //指定参数类型是输入参数还是输出参数
                cmd.Parameters["@pMob"].Direction = ParameterDirection.Input;
                cmd.Parameters["@pTimeType"].Direction = ParameterDirection.Input;
                cmd.Parameters["@opPwd"].Direction = ParameterDirection.Output;
                cmd.Parameters["@opMsg"].Direction = ParameterDirection.Output;
                cmd.Parameters["@opEndTimeNew"].Direction = ParameterDirection.Output;

                //执行存储过程 此处类似于查询语句
                cmd.ExecuteScalar();
                //接受执行存储过程后的返回值
                string endtime = "";
                if (cmd.Parameters["@opEndTimeNew"].Value!=null)
                {
                   DateTime tmptime = (DateTime)cmd.Parameters["@opEndTimeNew"].Value;
                   endtime = tmptime.ToString("yyyy-MM-dd");
                }
               //return endtime;
                reValue = (string)cmd.Parameters["@opPwd"].Value + "," + (string)cmd.Parameters["@opMsg"].Value+","+endtime;
                return reValue;
            }
            catch (Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "GetOrderByOrdernumber", LogYuMin.LogStates.ERROR, ex);
            }
            finally
            {
                try
                {
                    con.Close();
                }
                catch
                {
                }
            }
            return reValue;
        }


        /// <summary>
        /// 用户购买软件获取账号激活的密码及消息提示
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <param name="timetype">时间版本</param>
        /// <returns></returns>
        public string GetActiveUserPwd(string mobile,int timetype)
        {
            string reValue = "";
            
            SqlConnection con = new SqlConnection("server=192.168.10.252;database=FeeDB;uid=sa;pwd=emoney.cn;");
           //server=172.28.2.210;database=emoney_streak;uid=streak_web;pwd=$qta+=Kahrpakmx#c3;
            //SqlConnection con = new SqlConnection(" server=172.28.2.210;database=emoney_streak;uid=streak_web;pwd=$qta+=Kahrpakmx#c3");
            con.Open();
            try
            {
                SqlCommand cmd = new SqlCommand("QQ_AddFee", con); //10.252
                cmd.CommandType = CommandType.StoredProcedure;
                //添加存储过程输入输出参数类型及输入参数值
                cmd.Parameters.Add("@Mobile", SqlDbType.VarChar,11).Value = mobile;
                cmd.Parameters.Add("@months", SqlDbType.Int).Value = timetype;//10.252
                cmd.Parameters.Add("@opPwd", SqlDbType.VarChar, 8);
                cmd.Parameters.Add("@msgResult", SqlDbType.VarChar, 64);
                cmd.Parameters.Add("@opEndTimeNew", SqlDbType.DateTime);
                // cmd.Parameters["@username"].DbType = DbType.String;
                //指定参数类型是输入参数还是输出参数
                cmd.Parameters["@Mobile"].Direction = ParameterDirection.Input;
                cmd.Parameters["@months"].Direction = ParameterDirection.Input;
                cmd.Parameters["@opPwd"].Direction = ParameterDirection.Output;
                cmd.Parameters["@msgResult"].Direction = ParameterDirection.Output;
                cmd.Parameters["@opEndTimeNew"].Direction = ParameterDirection.Output;
                //执行存储过程 此处类似于查询语句
                cmd.ExecuteScalar();
                //接受执行存储过程后的返回值
                string endtime = "";
                if (cmd.Parameters["@opEndTimeNew"].Value != null)
                {
                    DateTime tmptime = (DateTime)cmd.Parameters["@opEndTimeNew"].Value;
                    endtime = tmptime.ToString("yyyy-MM-dd");
                }
                //return endtime;
                reValue = (string)cmd.Parameters["@opPwd"].Value + "," + (string)cmd.Parameters["@msgResult"].Value + "," + endtime;
                return reValue;
            }
            catch (Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "GetOrderByOrdernumber", LogYuMin.LogStates.ERROR, ex);
            }
            finally
            {
                try
                {
                    //reader.Close();
                    con.Close();
                }
                catch
                {
                }
            }
            return reValue;
        }
    }
}
